package cn.kust.microtourismcommon.util;

import cn.kust.microtourismcommon.pojo.dto.TokenResult;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;


import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WY
 * @data 2022/7/6 16:35
 **/
public class JwtUtils {

    private static final String SIGN = "youdontknown";

    // 所有用户的手机标识
    private static final String JWT_KEY_PHONE = "phone";

    // 用户id
    private static final String JWT_KEY_ID = "userId";

    // 乘客为1 司机为2
    private static final String JWT_KEY_IDENTITY = "identity";

    private static final String JWT_TOKEN_TYPE = "tokenType";

    //token生成的时间
    private static final String JWT_TOKEN_TIME = "tokenTime";

    // 生成

    /**
     *
     * @param phone  用户电话号码
     * @param identity 用户标识
     * @return
     */
    public static String generatorToken(String phone ,String identity,String userId ,String tokenType){
        //过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        Date date = calendar.getTime();
        JWTCreator.Builder builder = JWT.create();

        Map<String,String> map =new HashMap<>();
        map.put(JWT_KEY_ID,userId);
        map.put(JWT_KEY_PHONE,phone);
        map.put(JWT_KEY_IDENTITY,identity);
        map.put(JWT_TOKEN_TYPE, tokenType);
        map.put(JWT_TOKEN_TIME,date.toString());
        //标识
        map.forEach((key,value)->{
            builder.withClaim(key,value);
        });
        //有效期由redis来存
//        builder.withExpiresAt(date);

        //生成token
        String token = builder.sign(Algorithm.HMAC256(SIGN));

        return token;
    }
    //解析

    public static TokenResult parseToken(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        // 如果是toString的话会导致读取出来的值为“”
        String phone = verify.getClaim(JWT_KEY_PHONE).asString();
        String identity = verify.getClaim(JWT_KEY_IDENTITY).asString();

        TokenResult tokenResult = new TokenResult();
        tokenResult.setPhone(phone);
        tokenResult.setIdentity(identity);
        return tokenResult;
    }

    /**
     * 判断token是否合法
     * @param token
     * @return
     */
    public static TokenResult checkToken(String token){
        TokenResult tokenResult = null;
        try {
            tokenResult  = JwtUtils.parseToken(token);
        } catch (Exception e) {

        }
        return  tokenResult;
    }

    public static void main(String[] args) {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjE4MTIxMDkyNzM3IiwiaWRlbnRpdHkiOiIxIiwidG9rZW5UaW1lIjoiVGh1IEp1bCAyMSAyMToyMTozNCBDU1QgMjAyMiIsInRva2VuVHlwZSI6ImFjY2Vzc1Rva2VuIn0.4OGP1h5AosDYkH2gPGt7nGSDUWOZ2FvLtESLVy-fK5c";
        System.out.println("token:" + token);
        System.out.println("parse:" + parseToken(token) );

    }


}
